package com.dykj.fireFighting.web.base.dev.controller;

import java.util.*;

import com.dykj.fireFighting.api.base.devHisResult.entity.DevHisResult;
import com.dykj.fireFighting.api.base.devHisResult.entity.InstructionRecord;
import com.dykj.fireFighting.api.base.devHisResult.entity.TongBiBean;
import com.dykj.fireFighting.api.base.devHisResult.service.IDevHisResultService;
import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.exception.BaseException;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;


/**
 * 数据统计接口
 *
 * @Author LIJIANJIE
 * @Date  2021-09-22 10:24:54
 */
@Slf4j
@Api(value = "数据统计接口", tags = {"数据统计接口"})
@RestController
@RequestMapping("/devHisResult")
public class DevHisResultController {

    @DubboReference
    private IDevHisResultService devHisResultService;

    @DubboReference
    private IEnterpriseService enterpriseService;

    /**
     * 获取用户名称
     * @return String
     */
    public String getName() {
        try {
            return ShiroUtil.getUser().getName();
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 获取数据统计信息
     */
    @AutoLog(value = "数据统计-获取数据统计信息")
    @ApiOperation(value = "获取数据统计信息", notes = "获取数据统计信息")
    @GetMapping("/count")
    public CommonResult<DevHisResult> info(String unitId, Integer type, String beginTime, String endTime){
        DevHisResult devHisResult = null;
        // 社会单位
        if (type == 0) {
            if (StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime)) {
                devHisResult = devHisResultService.getUnitNum(unitId,beginTime,endTime);
            }else {
                devHisResult = devHisResultService.getUnitNum(unitId,null,null);
            }
        }
        // 监控中心
        if (type == 2) {
            // 获取所有单位
            List<Enterprise> lists = enterpriseService.queryUnitSelect(unitId);
            if (lists == null || lists.size() == 0) {
                return CommonResult.failed("该监控中心没有任何单位或代码错误，请联系管理员");
            }
            List<String> ids = new ArrayList<>();
            for (Enterprise list : lists ) {
                ids.add(list.getId() + "");
            }
            if (StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime)) {
                devHisResult = devHisResultService.getCenterNum(ids,beginTime,endTime);
            }else {
                devHisResult = devHisResultService.getCenterNum(ids,null,null);
            }
        }
        return CommonResult.success(devHisResult);
    }

    /**
     * 保存数据统计信息
     */
    @AutoLog(value = "数据统计-保存数据统计信息")
    @ApiOperation(value = "保存数据统计信息", notes = "保存数据统计信息")
    @PostMapping("/save")
    public synchronized CommonResult save(Integer type, String unitId, String date) {
        try {
            int b = devHisResultService.saveOne(type,unitId,date);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改指令执行记录信息
     */
    @AutoLog(value = "指令执行记录-修改指令执行记录信息")
    @ApiOperation(value = "修改指令执行记录信息", notes = "修改指令执行记录信息")
    @PostMapping("/update")
    public CommonResult update(@Valid @RequestBody InstructionRecord instructionRecord) {
        try {
            instructionRecord.setUpdateTime(new Date());
            instructionRecord.setIsDetermine(0);
            instructionRecord.setReceiveTime(new Date());
            instructionRecord.setUpdateBy(getName());
            devHisResultService.updateByUnitId(instructionRecord);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 按月查询当前监控中心下所有单位的同比数据
     */
    @AutoLog(value = "按月查询当前监控中心下所有单位的同比数据")
    @ApiOperation(value = "按月查询当前监控中心下所有单位的同比数据", notes = "按月查询当前监控中心下所有单位的同比数据")
    @GetMapping("/getTongBiByMonth")
    public CommonResult<List<TongBiBean>> getTongBiByMonth(String type,String newTime,String oldTime) {
        Long enterpriseId = ShiroUtil.getUser().getEnterpriseId();
        List<TongBiBean> tongBiBeanList =devHisResultService.getTongBiByMonth(type,newTime,oldTime,enterpriseId,null);
        return CommonResult.success(tongBiBeanList);
    }
}
